package com.factory.test.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.SparseArray;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseTestActivity;
import com.factory.test.databinding.ActivityTemperatureBinding;
import com.hqw.temp.QDTempModule;
import com.hqw.temp.TempModule;
import com.hqw.temp.TempModuleCallback;

public class TemperatureActivity extends BaseTestActivity {

    private ActivityTemperatureBinding binding;
    private TempModule tempModule;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_temperature);

        /**
         * GLH模组是全屏测温, 不支持指定区域测温
         */
        tempModule = new QDTempModule();   //测温模组实现类必须在主线程实例
        tempModule.init(context, null);
    }

    private TempModuleCallback tempModuleCallback = new TempModuleCallback() {
        @Override
        public void onReadInfo(String info) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    binding.message.append(info);
                }
            });
        }

        @Override
        public void onCallback(SparseArray<String> tempArray, final Bitmap thermalImaging) {
            final String temp = tempArray.get(0);
            handler.post(new Runnable() {
                @Override
                public void run() {
                    binding.tempText.setText(temp);
                    binding.tempImage.setImageBitmap(thermalImaging);
                }
            });
        }
    };


    @Override
    protected void onResume() {
        super.onResume();
        tempModule.start(tempModuleCallback);
    }

    @Override
    protected void onPause() {
        super.onPause();
        tempModule.stop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        tempModule.onDestroy();
    }
}
